package com.lmpop.trade.sms;

import com.lmpop.frame.util.Config;
import com.linkage.netmsg.NetMsgclient;
import com.linkage.netmsg.server.ReceiveMsg;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GroupMessager
{
  private Map customerInfo = null;
  private NetMsgclient client = null;
  private ReceiveMsg receiveMsg = null;
  private static GroupMessager instance = null;
  private String SMS_IPAddress = "";
  private int SMS_Port = 0;
  private String SMS_UserName = "";
  private String SMS_PassWord = "";
  private String SMS_Content = "";
  private Config config = null;
  String[] phoneNum = null;
  String[] userName = null;
  String[] order_no = null;
  String[] order_state = null;
  String[] userInfo = null;

  public GroupMessager() {
    this.config = new Config();
  }

  public static GroupMessager getInstance() {
    if (instance == null) {
      synchronized (GroupMessager.class) {
        if (instance == null) {
          instance = new GroupMessager();
          System.out.println("XXXXXXXXXXXXXXXX");
        }
      }
    }
    return instance;
  }

  public void init() {
    if (this.receiveMsg == null) {
      this.receiveMsg = new ReceiveDemo();
    }

    if (this.config.getString("SMS_IPAddress") != null) {
      this.SMS_IPAddress = this.config.getString("SMS_IPAddress");
    }
    if (this.config.getString("SMS_Port") != null) {
      this.SMS_Port = Integer.valueOf(this.config.getString("SMS_Port")).intValue();
    }
    if (this.config.getString("SMS_UserName") != null) {
      this.SMS_UserName = this.config.getString("SMS_UserName");
    }
    if (this.config.getString("SMS_PassWord") != null) {
      this.SMS_PassWord = this.config.getString("SMS_PassWord");
    }

    if (this.client == null)
      this.client = new NetMsgclient().initParameters(this.SMS_IPAddress, this.SMS_Port, this.SMS_UserName, this.SMS_PassWord, this.receiveMsg);
  }

  public void sendMessage(Object obj)
  {
    this.customerInfo = ((Hashtable)obj);
    try
    {
      boolean isLogin = this.client.anthenMsg(this.client);
      if (isLogin)
        System.out.println("login sucess");
      else {
        System.out.println("login faild");
      }

      if ((this.customerInfo != null) && (this.customerInfo.size() > 0)) {
        int size = Integer.valueOf(this.customerInfo.get("infoSize").toString()).intValue();
        for (int i = 0; i < size; i++) {
          if (this.customerInfo.get("SMS_Content") != null) {
            this.SMS_Content = this.customerInfo.get("SMS_Content").toString();
          }

          String phone = "";
          String name = "";
          if (this.customerInfo.get("MessageInfo_" + i) != null) {
            phone = this.customerInfo.get("MessageInfo_" + i).toString();
          }
          try
          {
            this.SMS_Content = new String(this.SMS_Content.getBytes("GBK"));
          } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(GroupMessager.class.getName()).log(Level.SEVERE, null, ex);
          }

          if ((phone == null) || 
            (phone.equals(""))) continue;
          System.out.println("-----phone-+SMS_Content--->" + phone + "--" + this.SMS_Content);
          this.client.sendMsg(this.client, 1, phone, this.SMS_Content, 0);
        }

      }

    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      System.exit(-1);
    } finally {
      if (this.client != null) {
        try {
          this.client.forceDisconnect();
        } catch (IOException ex) {
          Logger.getLogger(GroupMessager.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      System.exit(-1);
    }
  }

  public static void main(String[] args)
  {
    Map customerInfo = new Hashtable();
    int size = args.length;

    if (size == 1) {
      String[] all = args[0].split("\\/");
      if (all.length == 2) {
        String SMS_Content = all[0];
        String[] info = all[1].split("\\|");

        int infoSize = info.length;
        for (int i = 0; i < infoSize; i++) {
          customerInfo.put("MessageInfo_" + i, info[i]);
          System.out.println("-------------->" + info[i]);
        }
        customerInfo.put("infoSize", Integer.valueOf(infoSize));
        customerInfo.put("SMS_Content", SMS_Content);
      }
    }
    GroupMessager messageSender = getInstance();
    messageSender.init();
    messageSender.sendMessage(customerInfo);
  }
}